package com.core.service.Impl;

import com.core.dao.TmhDao;
import com.core.domain.DTO.StadiumDTO;
import com.core.domain.Stadium;
import com.core.domain.User;
import com.core.domain.useStadium;
import com.core.service.TmhService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author tmh
 * @date 2023/5/2 16:01
 * @description
 */
@Service
public class TmhServiceImpl implements TmhService {
    @Resource
    private TmhDao tmhDao;

    @Override
    public List<User> getListUsers() {
        return tmhDao.getListUsers();
    }

    @Override
    public void modifyPassword(Integer id, String newPassword) {
        tmhDao.modifyPassWord(id, newPassword);
    }

    @Override
    public User selectUserById(Integer id) {
        return tmhDao.selectUserById(id);
    }

    @Override
    public String convertToCsvUsers(List<User> dataList) {
        StringBuilder sb = new StringBuilder();
        // 写入表头
        sb.append("id,账号,密码,头像,性别,电话,邮箱,用户角色\n");
        // 写入数据行
        for (User user : dataList) {
            sb.append(user.getId()).append(",")
                    .append(user.getUserAccount()).append(",")
                    .append(user.getUserPassword()).append(",")
                    .append(user.getAvatarUrl()).append(",")
                    .append(user.getGender()).append(",")
                    .append(user.getPhone()).append(",")
                    .append(user.getEmail()).append(",")
                    .append(user.getUserRole()).append(",")
                    .append("\n");
        }
        return sb.toString();
    }

    @Override
    public String convertToCsvStadiumIncome(Map<String, Integer> map) {
        StringBuilder sb = new StringBuilder();
        sb.append("场馆名字,场馆收入\n");
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        for (Map.Entry<String, Integer> stadiumAndIncome : entrySet) {
            String stadiumName = stadiumAndIncome.getKey();
            Integer money = stadiumAndIncome.getValue();
            sb.append(stadiumName).append(",")
                    .append(money).append(",")
                    .append("\n");
        }
        return sb.toString();
    }

    @Override
    public String convertToCsvStadiumAndAge(List<Map<String, Object>> stadiumAndAgeList) {
        StringBuilder sb = new StringBuilder();
//        sb.append("场馆名称,年龄欢迎度\n");
        for (Map<String, Object> map : stadiumAndAgeList) {
            for (Map.Entry<String, Object> stadiumAndAgeMap : map.entrySet()) {
                String stadiumName = stadiumAndAgeMap.getKey();
                String age = (String) stadiumAndAgeMap.getValue();
                sb.append(stadiumName).append(",")
                        .append(age).append(",")
                        .append("\n");
            }
            sb.append("\n");
        }

        return sb.toString();
    }

    @Override
    public String convertToCsvStadiumAndPopular(List<Map<String, Object>> stadiumAndPopularList) {
        StringBuilder sb = new StringBuilder();
        sb.append("场馆名称,男性欢迎度,女性欢迎度\n");
        for (Map<String, Object> map : stadiumAndPopularList) {
            for (Map.Entry<String, Object> stadiumAndPolularMap : map.entrySet()) {
                String value = (String) stadiumAndPolularMap.getValue();
                sb.append(value).append(",");
            }
            sb.append("\n");
        }
        return sb.toString();
    }

    @Override
    public String convertToCsvStadiumUserNumbers(Map<String, Integer> useNumbers) {
        StringBuilder sb = new StringBuilder();
        sb.append("场馆名字,使用人数\n");
        for (Map.Entry<String, Integer> entry : useNumbers.entrySet()) {
            String stadiumName = entry.getKey();
            Integer useNumber = entry.getValue();
            sb.append(stadiumName).append(",")
                    .append(useNumber).append("\n");
        }
        return sb.toString();
    }

    @Override
    public String convertToCsvStadiumUtilizationRate(Map<String, String> stadiumUtilizationRate) {
        StringBuilder sb = new StringBuilder();
        sb.append("场馆类型,利用率\n");
        for (Map.Entry<String, String> entry : stadiumUtilizationRate.entrySet()) {
            String stadiumName = entry.getKey();
            String useNumber = entry.getValue();
            sb.append(stadiumName).append(",")
                    .append(useNumber).append("\n");
        }
        return sb.toString();

    }

    @Override
    public String convertToCsvQueryStadium(HashMap<useStadium, Integer> information) {
        StringBuilder sb = new StringBuilder();
        sb.append("用户名,场馆,预约开始时间,预约结束时间,创建时间,更新时间,是否预约,预约人数\n");
        for (Map.Entry<useStadium, Integer> useStadiumIntegerEntry : information.entrySet()) {
            useStadium key = useStadiumIntegerEntry.getKey();
            sb.append(key.getUserAccount()).append(",")
                    .append(key.getStadiumName()).append(",")
                    .append(key.getReservationStartTime()).append(",")
                    .append(key.getReservationEndTime()).append(",")
                    .append(key.getCreateTime()).append(",")
                    .append(key.getUpdateTime()).append(",")
                    .append(key.getIsDelete()).append(",")
                    .append(key.getReservedNumber()).append(",")
                    .append("\n");
        }
        sb.append("0代表:已经取消预约,1代表：已经预约");
        return sb.toString();
    }

    @Override
    public String convertToCsvQueryStadiumByName(Stadium stadium) {
        StringBuilder sb = new StringBuilder();
        sb.append("场馆名,场馆数量,场馆描述,开馆时间,闭关时间,租赁价格,最大限制人数,创建时间\n");
        sb.append(stadium.getStadiumName()).append(",")
                .append(stadium.getStadiumNum()).append(",")
                .append(stadium.getDescription()).append(",")
                .append(stadium.getStartTime()).append(",")
                .append(stadium.getEndTime()).append(",")
                .append(stadium.getRentalPrice()).append(",")
                .append(stadium.getLimitNum()).append(",")
                .append(stadium.getCreateTime()).append(",");
        return sb.toString();

    }

    @Override
    public String convertToCsvQueryUserInfo(User user) {
        StringBuilder sb = new StringBuilder();
        sb.append("用户名,年龄,性别,电话,邮箱\n");
        sb.append(user.getUserAccount()).append(",")
                .append(user.getAge()).append(",")
                .append(user.getGender()).append(",")
                .append(user.getPhone()).append(",")
                .append(user.getEmail()).append(",");
        return sb.toString();
    }

    @Override
    public String convertToCsvStadium(List<StadiumDTO> stadiumDTOList) {
        StringBuilder sb = new StringBuilder();
        sb.append("名称,开馆时间,闭馆时间,描述,价格,预约数量,总个数\n");
        for (StadiumDTO stadiumDTO : stadiumDTOList) {
            sb.append(stadiumDTO.getStadiumName()).append(",")
                    .append(stadiumDTO.getStartTime()).append(",")
                    .append(stadiumDTO.getEndTime()).append(",")
                    .append(stadiumDTO.getDescription()).append(",")
                    .append(stadiumDTO.getRentalPrice()).append(",")
                    .append(stadiumDTO.getCount()).append(",")
                    .append(stadiumDTO.getStadiumNum()).append(",")
                    .append("\n");
        }
        return sb.toString();
    }
}
